package com.senseuni.da.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.senseuni.common.dialog.CBDialogBuilder;
import com.senseuni.common.dialog.OnDialogResultListener;
import com.senseuni.common.utils.IMEUtil;
import com.senseuni.da.R;

public class WifiConnectDialog extends CBDialogBuilder {

    private EditText mEditText;
    private TextView mWifiSsidView;

    private boolean showPwd = false;


    public WifiConnectDialog(Context context) {
        super(context, CBDialogBuilder.DIALOG_STYLE_NORMAL, (float) 0.5);
        initView(context);
    }

    public WifiConnectDialog setHint(String hint) {
        if (mEditText != null) {
            mEditText.setHint(hint);
        }
        return this;
    }

    public WifiConnectDialog setSsid(String ssid) {
        if (mWifiSsidView != null) {
            mWifiSsidView.setText(ssid);
        }
        return this;
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_wifi_connect, null);
        mWifiSsidView = view.findViewById(R.id.tv_wifi_ssid);
        mEditText = view.findViewById(R.id.et_passwd);
        mEditText.requestFocus();
        View iconView = view.findViewById(R.id.view_show_hide_pwd);
        ImageView imageView = view.findViewById(R.id.iv_pwd_show_hide);
        imageView.setImageResource(R.drawable.ic_passwd_show);
        iconView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showPwd) {
                    showPwd = false;
                    mEditText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
                    imageView.setImageResource(R.drawable.ic_passwd_hide);
                } else {
                    showPwd = true;
                    mEditText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
                    imageView.setImageResource(R.drawable.ic_passwd_show);
                }
            }
        });

        setView(view);
        setTouchOutSideCancelable(true);
        showCancelButton(true);
        setButtonClickListener(false, (context1, dialog, whichBtn) -> {
            if (whichBtn == OnDialogBtnClickListener.BUTTON_CONFIRM) {
                String content = getEditContent();
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(context1.getApplicationContext(), context1.getText(R.string.passwd_empty), Toast.LENGTH_SHORT).show();
                    return;
                }

                if (content.length() < 8) {
                    Toast.makeText(context1.getApplicationContext(), context1.getText(R.string.passwd_short), Toast.LENGTH_SHORT).show();
                    return;
                }

                IMEUtil.hideSoftKeyboard(mEditText);
                if (mListener != null) {
                    mListener.onResult(dialog, content);
                }
            } else if (whichBtn == OnDialogBtnClickListener.BUTTON_CANCEL) {
                dialog.dismiss();
            }
        });
    }

    public String getEditContent() {
        return mEditText.getText().toString().trim();
    }

    private OnDialogResultListener<String> mListener;

    public void setOnResultListener(OnDialogResultListener<String> listener) {
        mListener = listener;
    }


}
